package com.mentormobile.database;

import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.mentormobile.version.CurrentVersion;

/**Esta classe e responsavel em abrir a conexao com o banco de dados, atualizar a estrutura
 * e criar os DAOS para cada model para alteracoes no banco de dados.*/
public class MentorMobileHelper extends OrmLiteSqliteOpenHelper {
	
	/** nome do banco de dados dentro do android*/
	private static final String DATABASE_NAME = "mentormobile.db";
	// we do this so there is only one helper

	public MentorMobileHelper(Context context) {
		super(context, DATABASE_NAME, null, CurrentVersion.getCurrentVersion().getSystemVersion().getDatabaseVersion());
	}

	/**
	 * This is called when the database is first created. Usually you should call createTable statements here to create
	 * the tables that will store your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		//	try {
		//	Log.i(DatabaseHelper.class.getName(), "onCreate");
			//	TableUtils.createTable(connectionSource, SimpleData.class);

			// here we try inserting data in the on-create as a test
			//	Dao<SimpleData, Integer> dao = getSimpleDataDao();
			//		long millis = System.currentTimeMillis();
			// create some entries in the onCreate
			//		SimpleData simple = new SimpleData(millis);
			//		dao.create(simple);
			//	simple = new SimpleData(millis + 1);
			//	dao.create(simple);
			//		//		Log.i(DatabaseHelper.class.getName(), "created new entries in onCreate: " + millis);
			//} catch (SQLException e) {
			//Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			//	throw new RuntimeException(e);
			//}
	}

	/**
	 * This is called when your application is upgraded and it has a higher version number. This allows you to adjust
	 * the various data to match the new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			CurrentVersion.getCurrentVersion().getSystemVersion().executeDDLDatabase(db, connectionSource, oldVersion, newVersion);
		} catch (SQLException e) {
			Log.e(this.getClass().getName(), "Can't update ddl database", e);
	     	throw new RuntimeException(e);
		}
		//	try {
		//		Log.i(DatabaseHelper.class.getName(), "onUpgrade");
		//	TableUtils.dropTable(connectionSource, SimpleData.class, true);
			// after we drop the old databases, we create the new ones
		//	onCreate(db, connectionSource);
		//} catch (SQLException e) {
		//		Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
		//	throw new RuntimeException(e);
		//}
	}


	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
	}
}
